//
//  EvaluateInsuredVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/5/13.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "EvaluateInsuredVC.h"
#import "HCSStarRatingView.h"
#import "IQTextView.h"

@interface EvaluateInsuredVC ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet HCSStarRatingView *starView;
@property (weak, nonatomic) IBOutlet UIView *desBackView;
@property (weak, nonatomic) IBOutlet UILabel *evaluateLabel;
@property (weak, nonatomic) IBOutlet IQTextView *contentLabel;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@end

@implementation EvaluateInsuredVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = kClearColor;
    self.starView.emptyStarColor = UIColorFromRGB(0xBFBFBF);
    self.starView.emptyStarImage = [UIImage imageNamed:@"ic_stars_没选中没选中Unselected"];
    self.starView.filledStarImage = [UIImage imageNamed:@"ic_stears_Selected"];
    self.starView.halfStarImage = [UIImage imageNamed:@"ic_stars_Halfselected"];
    self.starView.maximumValue = 5;
    self.starView.minimumValue = 0;
    self.starView.value = 5;
    self.starView.allowsHalfStars = NO;
    self.starView.accurateHalfStars = YES;
    self.starView.continuous = YES;
    [self.starView addTarget:self action:@selector(didChange:) forControlEvents:UIControlEventValueChanged];
    
    self.desBackView.layer.borderColor = UIColorFromRGB(0XA5B9C2).CGColor;
    self.desBackView.layer.borderWidth = 1;
    self.desBackView.layer.cornerRadius = 5;
    


}
- (void)textViewDidChange:(UITextView *)textView{
    if(textView.text.length > 100){
        ALERT_VIEW(@"不能超过100个字") ;
        return;
    }
    self.countLabel.text = [NSString stringWithFormat:@"%zd/100",textView.text.length];
}
- (void)didChange:(HCSStarRatingView*)sender{
    NSLog(@"starView.value:%f",sender.value);
    self.evaluateLabel.textColor = UIColorFromRGB(0XFEC324);
    if(sender.value <=3){
        self.evaluateLabel.text = @"一般，差强人意";
        self.evaluateLabel.textColor = UIColorFromRGB(0XA5B9C2);
    }else if(sender.value == 4){
        self.evaluateLabel.text = @"满意，物有所值";
    }else if(sender.value == 5){
        self.evaluateLabel.text = @"非常满意，无可挑剔。";
    }
}
- (IBAction)submitLabel:(id)sender {
    if(self.contentLabel.text.length > 100){
        ALERT_VIEW(@"不能超过100个字") ;
        return;
    }
    NSString *url = @"";
    NSString *key = @"";
    if (self.type == 0) {
        url = @"assurePass/evaluate";
        key = @"assurePassId";
    }else if (self.type == 1) {
        url = @"decrypt/evaluate";
        key = @"decryptId";
    }else if (self.type == 2) {
        url = @"question/evaluate";
        key = @"questionId";
    }
    if (self.type == 2) {
        [self showProgressWithView:self.view animated:YES];
        [YJMSendRequest postDataWithParameters:@{@"questionId":[NSString stringWithFormat:@"%@",self.relatedId],@"anwserId":[NSString stringWithFormat:@"%zd",self.anwserId]} url:@"question/acceptAnwser" result:^(NSDictionary *result, NSError *error) {
            result = [CommonUtils decryptDicWithResult:result];
            [self hideProgress:self.view animated:YES];
            if([result[@"result"] integerValue] == 1)
            {
                NSMutableDictionary *dicInfoParams = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
                                                      [NSString stringWithFormat:@"%@",self.relatedId],key,
                                                      self.contentLabel.text,@"content",
                                                      [NSString stringWithFormat:@"%.0f",self.starView.value],@"star",
                                                      nil];
                
                
                
                [YJMSendRequest postDataWithParameters:dicInfoParams url:url result:^(NSDictionary *result, NSError *error) {
                    result = [CommonUtils decryptDicWithResult:result];
                    [self hideProgress:self.view animated:YES];
                    if([result[@"result"] integerValue] == 1)
                    {
                        [self dismissViewControllerAnimated:YES completion:nil];
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadBuyInsureOrder" object:nil ];
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshHelpList" object:nil ];
                        ALERT_VIEW(@"评价成功") ;
                    }else{
                        [CommonUtils showToast:[result objectForKey:@"msg"]];
                    }
                    
                }];
            }else{
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
            
        }];
        
        
    }else{
        NSMutableDictionary *dicInfoParams = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
                                              [NSString stringWithFormat:@"%@",self.relatedId],key,
                                              self.contentLabel.text,@"content",
                                              [NSString stringWithFormat:@"%.0f",self.starView.value],@"star",
                                              nil];
        
        
        [self showProgressWithView:self.view animated:YES];
        [YJMSendRequest postDataWithParameters:dicInfoParams url:url result:^(NSDictionary *result, NSError *error) {
            result = [CommonUtils decryptDicWithResult:result];
            [self hideProgress:self.view animated:YES];
            if([result[@"result"] integerValue] == 1)
            {
                [self dismissViewControllerAnimated:YES completion:nil];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadBuyInsureOrder" object:nil ];
                ALERT_VIEW(@"评价成功") ;
            }else{
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
            
        }];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)closeClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
